package com.aos.erp.schools.service.registerschool.impl;


import com.aos.erp.common.bean.IPageBean;
import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.common.constant.GlobalConstant;
import com.aos.erp.schools.mapper.registerschool.RegisterSchoolMapper;
import com.aos.erp.schools.model.registerschool.entity.RegisterSchool;
import com.aos.erp.schools.model.registerschool.ro.RegisterSchoolRo;
import com.aos.erp.schools.model.registerschool.ro.UpdateRegisterSchoolRo;
import com.aos.erp.schools.model.registerschool.vo.ListRegisterSchoolVo;
import com.aos.erp.schools.model.registerschool.vo.RegisterSchoolVo;
import com.aos.erp.schools.service.registerschool.RegisterSchoolService;
import com.aos.erp.schools.service.sys.DepartmentService;
import com.aos.erp.schools.util.UserUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 报名点 服务实现类
 * </p>
 *
 * @author ywb
 * @since 2018-10-31
 */
@Service
public class RegisterSchoolServiceImpl extends ServiceImpl<RegisterSchoolMapper, RegisterSchool> implements RegisterSchoolService {

	@Resource
	private RegisterSchoolMapper registerSchoolMapper;

	@Resource
	private DepartmentService departmentService;

	@Resource
	GlobalConstant globalConstant;

	@Override
	public int addRegister(RegisterSchoolRo registerSchoolRo) throws Exception {

		//根据上级驾校Id获取省市，和车管所id
		RegisterSchool parentSchool = baseMapper.selectById(registerSchoolRo.getParentId());

		/////////////////////////////////
		//获取depCode
		String depCode=departmentService.getDepCodeBranch(parentSchool.getDepCode(),"B");

		//转换ro
		RegisterSchool registerSchool=new RegisterSchool();
		BeanUtils.copyProperties(registerSchoolRo,registerSchool);
		Date date=new Date();
		registerSchool.setDivisionId(parentSchool.getDivisionId());
		registerSchool.setProvinceId(parentSchool.getProvinceId());
		registerSchool.setCityId(parentSchool.getCityId());
		registerSchool.setAreaId(parentSchool.getAreaId());
		registerSchool.setType(4);//为报名点
		registerSchool.setRemark("系统添加报名点");
		registerSchool.setDepCode(depCode);
		registerSchool.setCreated(date);
		registerSchool.setUpdated(date);
		registerSchool.setIsDel(0);//默认不删除
		registerSchool.setStatus(0);
		registerSchool.setCreateUser(Long.parseLong(registerSchoolRo.getCurrentUserId()));
		int i=baseMapper.insert(registerSchool);
		if(i==1){
			//新增组织
			departmentService.addBeanSchoolBranch(parentSchool.getDepCode(),depCode,registerSchoolRo.getName(),"B",globalConstant.getAPP_CODE_SCHOOL());
		}
		return i;
	}

	@Override
	public ResultEntity deleteRegister(Long id) {
		RegisterSchool registerSchool=this.getById(id);
		if(registerSchool==null){
			return ResultEntity.failed("报名点错误");
		}
		registerSchool.setIsDel(-1);//设置为逻辑删除
		this.saveOrUpdate(registerSchool);
		return ResultEntity.success();
	}

	@Override
	public ResultEntity registerInfo(Long id) {
		RegisterSchool registerSchool=baseMapper.selectById(id);
		RegisterSchool parentSchool=baseMapper.selectById(registerSchool.getParentId());
		RegisterSchoolVo registerSchoolVo=new RegisterSchoolVo();
		if(parentSchool.getType()==1||parentSchool.getType()==2){
			registerSchoolVo.setGatherSchoolId(parentSchool.getId());
			registerSchoolVo.setBranchSchoolId(null);
		}else if(parentSchool.getType()==3){
			//如果上级市分校，则再查总校
			RegisterSchool gatherSchool=baseMapper.selectById(parentSchool.getParentId());
			registerSchoolVo.setGatherSchoolId(gatherSchool.getId());
			registerSchoolVo.setBranchSchoolId(parentSchool.getId());
		}
		BeanUtils.copyProperties(registerSchool,registerSchoolVo);
		return ResultEntity.success(registerSchoolVo);
	}

	@Override
	public ResultEntity updateRegister(UpdateRegisterSchoolRo updateRegisterSchoolRo) {
		//根据上级驾校Id获取省市，和车管所id
		RegisterSchool parentSchool = baseMapper.selectById(updateRegisterSchoolRo.getParentId());
		RegisterSchool registerSchool=new RegisterSchool();
		BeanUtils.copyProperties(updateRegisterSchoolRo,registerSchool);
		registerSchool.setDivisionId(parentSchool.getDivisionId());
		registerSchool.setProvinceId(parentSchool.getProvinceId());
		registerSchool.setCityId(parentSchool.getCityId());
		registerSchool.setAreaId(parentSchool.getAreaId());
		registerSchool.setUpdated(new Date());
		int count = registerSchoolMapper.updateRegister(registerSchool);
		return ResultEntity.success();
	}

	@Override
	public IPageBean queryByPage(Integer pageIndex, Integer pageSize, String name,String currentUserId) throws Exception {
		IPage<ListRegisterSchoolVo> page = new Page<>(pageIndex==null?1:pageIndex,pageSize==null?10:pageSize);
		String schoolId=UserUtil.getUserSchoolId(currentUserId);
		RegisterSchool RegisterSchool=this.baseMapper.selectById(schoolId);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("schoolId",schoolId);
		map.put("depCode",RegisterSchool.getDepCode());
		map.put("name",name);
		map.put("type",4);//报名点类型
		map.put("IPage",page);
		List<ListRegisterSchoolVo> list=registerSchoolMapper.queryList(map);
		page.setRecords(list);
		return baseMapper.toBizPage(page);
	}
}
